﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Windows.Controls.Primitives;

namespace SocketClient
{
    /// <summary>
    /// Interaction logic for menu.xaml
    /// </summary>
    public partial class menu : Window
    {
        private Conexao conexao;
        Thread threadSom;
        private MediaPlayer mp = new MediaPlayer();

        public menu(Conexao con)
        {
            this.conexao = con;
            InitializeComponent();

            string diretorio = Environment.CurrentDirectory;
            string[] dividis = diretorio.Split('\\');
            string nvdiretorio = "";
            for (int i = 0; i < dividis.Length - 2; i++)
                nvdiretorio = nvdiretorio + dividis[i] + "/";
            nvdiretorio = nvdiretorio + "Sons/troops coming with their bugs.mp3";
            mp.Open(new Uri(nvdiretorio));
            /*threadSom = new Thread(new ThreadStart(TocaSom));
            threadSom.IsBackground = true;
            threadSom.Start();*/
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            conexao.Send("LOGOUT");
            mp.Stop();
            this.Closed += (sender2, e2) =>
      this.Dispatcher.InvokeShutdown();

            base.OnClosing(e);
        }

        public void TocaSom()
        {
            Dispatcher.BeginInvoke(
                               new ThreadStart(() =>
                               {
                                   mp.Play();
                               }));
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            conexao.Send("LOGOUT");
            mp.Stop();

            this.Closed += (sender2, e2) =>
this.Dispatcher.InvokeShutdown();

            Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            criarsala cs = new criarsala(conexao);
            cs.ShowDialog();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            visualizarsala vs = new visualizarsala(conexao);
            vs.ShowDialog();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (button4.Content.ToString() == "On")
            {
                mp.Stop();
                button4.Content = "Off";
            }
            else if (button4.Content.ToString() == "Off")
            {
                threadSom = new Thread(new ThreadStart(TocaSom));
                threadSom.IsBackground = true;
                threadSom.Start();
                button4.Content = "On";
            }
        }
    }
}
